package com.zd.bms.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zd.bms.bean.vo.req.TicketInfoListReqVo;
import com.zd.bms.bean.vo.resp.TicketInfoRespVo;
import com.zd.bms.service.ITicketInfoService;
import com.zd.common.bean.model.BmsUser;
import com.zd.common.bean.model.TicketInfo;
import com.zd.common.dao.common.Page;

/**
 * 票类型controller
 * 
 * @author lishi
 * @date 2018年11月22日 上午10:21:17
 */
@Controller
@RequestMapping("ticketInfo")
public class TicketInfoController extends BaseController {
	@Autowired
	private ITicketInfoService ticketInfoService;

	@RequestMapping("index")
	public ModelAndView index() throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("ticketInfo/index");
		return mv;
	}

	@RequestMapping("saveOrUpdateIndex")
	public ModelAndView saveOrUpdateIndex(Integer ticketInfoId) throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("ticketInfo/saveOrUpdateIndex");
		//校验数据是否合法
		BmsUser bmsUser = getSessionUser();
		Integer scenicSpotId = bmsUser.getScenicSpotId();

		if (ticketInfoId != null) {
			TicketInfo ticketInfo = ticketInfoService.get(ticketInfoId);
			if (scenicSpotId != null && scenicSpotId != 0) {
				if (scenicSpotId.equals(ticketInfo.getScenicSpotId())) {
					//TODO 跳转到没有权限页面
				}
			}
			TicketInfoRespVo vo = new TicketInfoRespVo();
			BeanUtils.copyProperties(ticketInfo, vo);
			mv.addObject("ticketInfo", vo);
		}
		mv.addObject("scenicSpotId", scenicSpotId);
		return mv;
	}

	@ResponseBody
	@RequestMapping("list")
	public Map<String, Object> list(TicketInfoListReqVo reqVo) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		BmsUser bmsUser = getSessionUser();
		Integer scenicSpotId = bmsUser.getScenicSpotId();
		if (scenicSpotId != null && scenicSpotId != 0) {
			reqVo.setScenicSpotId(scenicSpotId);
		}
		Page<TicketInfo> ticketInfoPage = ticketInfoService.pageTicketInfo(reqVo);

		List<TicketInfo> voList = ticketInfoPage.getList();
		List<TicketInfoRespVo> respVoList = new ArrayList<>();
		if (voList != null && voList.size() > 0) {
			for (TicketInfo ticketInfo : voList) {
				TicketInfoRespVo respVo = new TicketInfoRespVo();
				BeanUtils.copyProperties(ticketInfo, respVo);
				respVoList.add(respVo);
			}
		}

		resultMap.put("data", respVoList);
		resultMap.put("draw", reqVo.getDraw());
		resultMap.put("recordsTotal", respVoList.size());
		resultMap.put("recordsFiltered", ticketInfoPage.getTotalRow());
		return resultMap;
	}

	@ResponseBody
	@RequestMapping("saveOrUpdate")
	public Map<String, Object> saveOrUpdate(TicketInfo ticketInfo) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		BmsUser bmsUser = getSessionUser();
		Integer scenicSpotId = bmsUser.getScenicSpotId();
		if (scenicSpotId != null && scenicSpotId != 0 && scenicSpotId != ticketInfo.getScenicSpotId()) {
			resultMap.put("result", false);
			resultMap.put("errorMsg", "不能操作其它景区的门票!");
			return resultMap;
		}
		TicketInfo originTicketInfo = null;
		if (ticketInfo.getId() != null) {
			originTicketInfo = ticketInfoService.get(ticketInfo.getId());
			if (originTicketInfo.getScenicSpotId() != ticketInfo.getScenicSpotId()) {
				resultMap.put("result", false);
				resultMap.put("errorMsg", "非法数据!");
				return resultMap;
			}
			BeanUtils.copyProperties(ticketInfo, originTicketInfo, "status", "createTime", "updateTime");
			originTicketInfo.setUpdateTime(new Date());
			ticketInfoService.update(originTicketInfo);
		} else {
			originTicketInfo = new TicketInfo();
			BeanUtils.copyProperties(ticketInfo, originTicketInfo, "status", "createTime", "updateTime");
			originTicketInfo.setCreateTime(new Date());
			originTicketInfo.setUpdateTime(new Date());
			ticketInfoService.insert(originTicketInfo);
		}
		resultMap.put("result", true);
		return resultMap;
	}

	@ResponseBody
	@RequestMapping("changeStatus")
	public Map<String, Object> changeStatus(Integer ticketInfoId, int status) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		BmsUser bmsUser = getSessionUser();
		Integer scenicSpotId = bmsUser.getScenicSpotId();
		if (ticketInfoId != null && ticketInfoId != 0) {
			TicketInfo ticketInfo = ticketInfoService.get(ticketInfoId);
			if (ticketInfo != null) {
				if(scenicSpotId != null && scenicSpotId != 0 && scenicSpotId != ticketInfo.getScenicSpotId()) {
					resultMap.put("result", false);
					resultMap.put("errorMsg", "不能操作其它景区的门票!");
					return resultMap;
				} 
				ticketInfo.setStatus(status);
				ticketInfo.setUpdateTime(new Date());
				ticketInfoService.update(ticketInfo);
				resultMap.put("result", true);
				return resultMap;
			}
		}
		resultMap.put("result", false);
		resultMap.put("errorMsg", "票不存在!");
		return resultMap;
	}
}
